package com.example.finalframe.controller;

import com.example.finalframe.aop.Authority;
import com.example.finalframe.common.ApiConstant;
import com.example.finalframe.common.ApiResult;
import com.example.finalframe.common.Constant;
import com.example.finalframe.model.dto.MenuDto;
import com.example.finalframe.model.dto.UserDto;
import com.example.finalframe.model.query.RoleMenuIdQuery;
import com.example.finalframe.model.query.UserQuery;
import com.example.finalframe.model.vo.UserVo;
import com.example.finalframe.service.factory.LoginFactory;
import com.example.finalframe.service.intf.LoginIntf;
import com.example.finalframe.service.intf.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;


@RestController
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    LoginFactory loginFactory;

    @PostMapping("/api/login")
    public ApiResult<UserVo> apilogin(@RequestBody UserQuery userQuery, @RequestHeader(value="client_type",required=false,defaultValue= Constant.CLIENT_TYPE_WEB) String clientType) throws UnsupportedEncodingException {
        System.out.println("username=" + userQuery.getUsername() + ",password=" + userQuery.getPassword());
        UserVo userVo = new UserVo();

        LoginIntf loginIntf = loginFactory.createLogin("UsernamePassword");
        UserDto userDto = loginIntf.login(userQuery.getUsername(), userQuery.getPassword());
        if (userDto != null) {
            String token = userService.genarateToken(userDto.getId(),userQuery.getPassword(),clientType);
            BeanUtils.copyProperties(userDto, userVo);
            userVo.setToken(token);
            return ApiResult.success(userVo);
        }
        return ApiResult.fail(ApiConstant.ERR_LOGIN_FAIL,"用户名或者密码错误");
    }

    @Authority("0")
    @GetMapping("/api/roles")
    public ApiResult<String> getUserRole(HttpServletRequest request){
        return ApiResult.success("管理员权限");
    }


    @GetMapping("/api/menus")
    public ApiResult< ArrayList<List<MenuDto>>> getAdminMenus(HttpServletRequest request){
        long userId = Long.parseLong(String.valueOf(request.getAttribute("userId")));
        Integer id = (int) userId;
        Integer roleid = userService.findRoleId(id);
        List<MenuDto> menuDtoList = userService.findMenuListByRoleId(roleid);
        ArrayList<List<MenuDto>> menuTypeList = new ArrayList<List<MenuDto>>();
        menuTypeList.add(new ArrayList<MenuDto>());
        menuTypeList.add(new ArrayList<MenuDto>());
        menuTypeList.add(new ArrayList<MenuDto>());
        for( MenuDto m: menuDtoList) {
            if(m.getMenutype() == 1) {
                menuTypeList.get(0).add(m);
            }else if(m.getMenutype() == 2) {
                menuTypeList.get(1).add(m);
            }else if(m.getMenutype() == 3) {
                menuTypeList.get(2).add(m);
            }
        }
        if(menuTypeList != null) {
            return ApiResult.success(menuTypeList);
        }
        return ApiResult.fail(ApiConstant.RESULT_OTHER_EXCEPTION,"数据为空");
    }

    //获取所有菜单项
    @Authority("0")
    @GetMapping("/api/allMenus")
    public ApiResult<List<MenuDto>> getAllMenus(HttpServletRequest request){
        List<MenuDto> menuDtoList = userService.findMenuList();
        if(menuDtoList != null) {
            return ApiResult.success(menuDtoList);
        }
        return ApiResult.fail(ApiConstant.RESULT_OTHER_EXCEPTION,"数据为空");
    }

    //添加用户权限
    @Authority("0")
    @PostMapping("/api/addMenu")
    public boolean addMenuForRole(@RequestBody RoleMenuIdQuery roleMenuIdQuery){
        return userService.addMenuForRole(roleMenuIdQuery.getRoleid(),roleMenuIdQuery.getMenuid());
    }

    //删除用户权限
    @Authority("0")
    @PostMapping("/api/deleteMenu")
    public boolean deleteMenuForRole(@RequestBody RoleMenuIdQuery roleMenuIdQuery){
        return userService.deleteMenuForRole(roleMenuIdQuery.getRoleid(),roleMenuIdQuery.getMenuid());
    }
}
